Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to add extension from the CommonMarkConverter class #1052

Open
wants to merge 1 commit into
base: 2.5
Choose a base branch
from

Conversation

Pierstoval
Copy link

In my use-case, the application is using CommonMark in a non-usual way, and I just need to add extensions to it and append configuration to the Environment (it's in a Symfony context).

However, considering the current architecture, there are only two possibilities for the package in Symfony DI's setup:

  • Use the LeagueCommonMarkConverterFactory from TwigExtraBundle. You can add extensions (they're injected in the constructor, and services can just be tagged, but you cannot add any configuration to the array $config constructor parameter.
  • Create a standard DI's Definition with any DI way (I'm using Yaml, but all other formats are concerned). This allows adding custom configuration to the constructor. But to add extensions, you have to call $converter->getEnvironment()->addExtension(), which is not possible with Symfony's DI.

For now, there is no way to do it, either from TwigExtra or from CommonMark itself.

I will also make a PR on TwigExtra to allow better customization of the factory itself, for more flexibility

@colinodell
Copy link
Member

Hi @Pierstoval,

Create a standard DI's Definition with any DI way
...
For now, there is no way to do it

Have you looked at using the MarkdownConverter instead? It's the base class for CommonMarkConverter and is designed for this exact use case! 🙂

You'd need two DI definitions:

  • One for the Environment
    • Pass the config array into the constructor
    • Add calls to the addExtension() method
  • One for the MarkdownConverter
    • Pass a reference to the Environment service into the constructor

Give that a try and let me know if that works for you.

@Pierstoval
Copy link
Author

Pierstoval commented Sep 25, 2024

@colinodell The problem in that case mostly comes from TwigExtra, since the Twig\Extra\Markdown\LeagueMarkdown class depends specifically on League\CommonMark\CommonMarkConverter.

I can indeed create a definition for the League\CommonMark\Environment\Environment class, but as said in the first post, there is no way to inject it in League\CommonMark\CommonMarkConverter. In this PR I had only two choices: allow adding extensions from the Converter (as a wrapper), or add an optional Environment parameter to the constructor. Since the config is already there, I decided it was less costly to execute ->addExtension later (since Symfony DI allows it)

Edit: I also wanted to locally extend CommonMarkConverter to at least find a workaround, but it's final 🙈

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants